import Vue from "vue"
import Vuex from 'vuex'
import { getUserInfo, register, logout, login } from '@/api/login'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        id: '',
        account: '',
        name: '',
        avatar: '',
        token: localStorage.token,
    },
    mutations: {
        SET_TOKEN(state, token) {
            state.token = token
        },
        SET_ACCOUNT(state, account) {
            state.account = account
        },
        SET_NAME(state, name) {
            state.name = name
        },
        SET_AVATAT(state, avatar) {
            state.avatar = avatar
        },
        SET_ID(state, id) {
            state.id = id
        }
    },
    actions: {
        register(context, user) {
            return new Promise((resolve, reject) => {
                register(user).then((res) => {
                    if (res.data.success) {
                        context.commit('SET_TOKEN', res.data.data)
                        localStorage.token = res.data.data
                        resolve()
                    } else {
                        reject(res.data.msg)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        getUserInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getUserInfo().then((res) => {
                    if (res.data.success) {
                        commit('SET_ACCOUNT', res.data.data.account)
                        commit('SET_NAME', res.data.data.nickname)
                        commit('SET_AVATAR', res.data.data.avatar)
                        commit('SET_ID', res.data.data.id)
                    } else {
                        commit('SET_ACCOUNT', '')
                        commit('SET_NAME', '')
                        commit('SET_AVATAR', '')
                        commit('SET_ID', '')
                        commit('SET_TOKEN', '')
                        reject(res.data.msg)
                    }
                }).catch((error) => {
                    commit('SET_ACCOUNT', '')
                    commit('SET_NAME', '')
                    commit('SET_AVATAR', '')
                    commit('SET_ID', '')
                    commit('SET_TOKEN', '')
                    reject(error)
                })
            })
        },
        logout({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(res => {
                    if (res.data.success) {
                        commit('SET_TOKEN', '')
                        commit('SET_ACCOUNT', '')
                        commit('SET_NAME', '')
                        commit('SET_AVATAR', '')
                        commit('SET_ID', '')
                        localStorage.removeItem('token')
                        resolve()
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        login({ commit }, user) {
            return new Promise((resolve, reject) => {
                login(user).then(res => {
                    if (res.data.success) {
                        commit('SET_TOKEN', res.data.data)
                        localStorage.token = res.data.data;
                        resolve()
                    } else {
                        reject(res.data.msg)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
})

export default store